package com.cbl.v.ssczs.view.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cbl.v.ssczs.R;
import com.cbl.v.ssczs.mode.entity.ItemBean;
import com.cbl.v.ssczs.mode.entity.LocalDataBean;
import com.cbl.v.ssczs.view.defindview.ShowAllGridView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 王凡 on 2017/8/27.
 */

public class ItemAdapter extends MyBaseAdapter<ItemBean> {
    private LocalDataBean localDataBean;
    private String type;
    private int style=0;
    public ItemAdapter(Context context, List<ItemBean> list) {
        super(context, list);
    }

    public void setLocalDataBean(LocalDataBean localDataBean) {
        this.localDataBean = localDataBean;
    }

    public void setType(String type){
        this.type=type;
    }

    public void setStyle(int style){
        this.style=style;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            switch (style){
                case 0:
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout, null);
                    break;
                case 1:
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout_style_one, null);
                    break;
            }
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        List<TextView> viewList = new ArrayList<>();
        final ItemBean bean = dataList.get(position);
        holder.tvNumber.setText("第" + bean.getExpect() + "期");
        holder.tvTime.setText(bean.getOpentime());
        List<String> ballList = Arrays.asList(bean.getOpencode().replace("+", ",").split(","));

        Collections.addAll(viewList, holder.tvNumberOne, holder.tvNumberTwo, holder.tvNumberThree, holder.tvNumberFour, holder.tvNumberFive,
                holder.tvNumberSix,holder.tvNumberSeven,holder.tvNumberEight,holder.tvNumberNine,holder.tvNumberTen);

        for (int i = 0; i < ballList.size(); i++) {
            TextView tvNumber = viewList.get(i);
            tvNumber.setVisibility(View.VISIBLE);
            tvNumber.setText(ballList.get(i));
        }
        if (localDataBean!=null){
            String title=localDataBean.getTitle();
            if (title.contains("双色球")){
                holder.tvNumberSeven.setBackground(getActivity().getResources().getDrawable(R.mipmap.ic_circle_blue_bg));
            }else if (title.contains("大乐透")){
                holder.tvNumberSix.setBackground(getActivity().getResources().getDrawable(R.mipmap.ic_circle_blue_bg));
                holder.tvNumberSeven.setBackground(getActivity().getResources().getDrawable(R.mipmap.ic_circle_blue_bg));
            }else if (title.contains("七乐彩")){
                holder.tvNumberEight.setBackground(getActivity().getResources().getDrawable(R.mipmap.ic_circle_blue_bg));
            }else {
                holder.tvNumberSeven.setBackground(getActivity().getResources().getDrawable(R.mipmap.ic_circle_red_bg));
            }
        }

        if (!TextUtils.isEmpty(type)&&type.contains("双色球")){
            holder.tvNumberSeven.setBackground(getActivity().getResources().getDrawable(R.mipmap.ic_circle_blue_bg));
        }

        return convertView;
    }


    class ViewHolder {
        @BindView(R.id.tv_number)
        TextView tvNumber;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.gv_ball)
        ShowAllGridView gvBall;
        @BindView(R.id.tv_number_one)
        TextView tvNumberOne;
        @BindView(R.id.tv_number_two)
        TextView tvNumberTwo;
        @BindView(R.id.tv_number_three)
        TextView tvNumberThree;
        @BindView(R.id.tv_number_four)
        TextView tvNumberFour;
        @BindView(R.id.tv_number_five)
        TextView tvNumberFive;
        @BindView(R.id.tv_number_six)
        TextView tvNumberSix;
        @BindView(R.id.tv_number_seven)
        TextView tvNumberSeven;
        @BindView(R.id.tv_number_eight)
        TextView tvNumberEight;
        @BindView(R.id.tv_number_nine)
        TextView tvNumberNine;
        @BindView(R.id.tv_number_ten)
        TextView tvNumberTen;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
